package org.mm.fastdfs.fastdfs.controller;

import lombok.extern.slf4j.Slf4j;
import org.csource.common.Base64;
import org.csource.common.NameValuePair;
import org.mm.fastdfs.fastdfs.service.FastDfsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Slf4j
public class IndexController {

    @Autowired
    private FastDfsService fastDfsService;

    @RequestMapping("/upload")
    public String[] upload(){
        String filePath ="/Users/zhudandan/Downloads/9c40a09ab9.jpg";
        NameValuePair nameValuePair= new NameValuePair("age","19");
        String fileExt="jpg";
        String[] str = this.fastDfsService.uploadFile(filePath,fileExt,new NameValuePair[]{nameValuePair});
        log.info(str.toString());
        return str;
    }

    @RequestMapping("/download")
    public String download(@RequestParam("groupName") String groupName,
                           @RequestParam("remotePath") String remotePath) throws IOException {
        return new Base64().encode(this.fastDfsService.downloadFile(groupName,remotePath));

    }

    @RequestMapping("/info")
    public String info(@RequestParam("groupName") String groupName,
                           @RequestParam("remotePath") String remotePath) throws IOException {
        return this.fastDfsService.getFileInfo(groupName,remotePath).toString();

    }

    @RequestMapping("/meta")
    public String meta(@RequestParam("groupName") String groupName,
                           @RequestParam("remotePath") String remotePath) throws IOException {
        return this.fastDfsService.getFileMate(groupName,remotePath).toString();

    }
    @RequestMapping("/del")
    public boolean del(@RequestParam("groupName") String groupName,
                       @RequestParam("remotePath") String remotePath) throws IOException {
        return this.fastDfsService.deleteFile(groupName,remotePath);

    }


}
